package com.lsm.czxxgl_system.controller;

import com.lsm.czxxgl_system.model.domain.Czylb;
import com.lsm.czxxgl_system.model.domain.User;
import com.lsm.czxxgl_system.model.request.CzSearchRequest;
import com.lsm.czxxgl_system.model.request.UserSearchRequest;
import com.lsm.czxxgl_system.service.CzylbService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.stream.Collectors;

/**
 * ClassName: CzglController
 * Package: com.lsm.czxxgl_system.controller
 * Description:
 *
 * @Author lsm
 * @Create 2024/6/27 14:05
 * @Version 1.0
 */
@Controller
@RequestMapping("/czgl")
public class CzglController {
    @Autowired
    CzylbService czylbService;

    @GetMapping("/list")
    public String list(Model model) {
        //获取测站信息
        List<Czylb> lists = czylbService.list();
        //保存到请求域
        model.addAttribute("czxx",lists);
        return "table/czxx_table";
    }
    @GetMapping("/getAdd")
    public String getAdd() {
        return "add/add_sta";
    }
    @GetMapping("/get/{name}/{code}")
    public String getByNameAndCode(@PathVariable String name, @PathVariable String code,Model model) {
        //判断主键是否有一个为空
        if (StringUtils.isAnyBlank(name,code)) {
            //查询不到数据，直接返回
            return null;
        }
        //根据主键查询测站
        Czylb czylb = czylbService.searchByPriKey(name,code);
        model.addAttribute("czxx",czylb);
        return "update/update_sta";

    }


    @PostMapping("/add")
    public String add(Czylb czyl, HttpServletRequest httpServletRequest) {
        //验证角色权限 是否为管理员

        //判断参数是否为空
        if (czyl == null) {
            return null;
        }
        //判断用户是否为空
        if (!czylbService.saveCzyl(czyl)) {
            //用户为空，返回添加测站将页面
            return "add/add_sta";
        }
        //返回用户信息页面
        return "redirect:/czgl/list";
    }


    @PutMapping("/update")
    public String update(Czylb czylb,String objCodeOri,String objNameOri) {
        //判断参数是否为空
        if (czylb == null) {
            return null;
        }
        //修改用户信息
        if (!czylbService.updateCzxx(czylb,objCodeOri,objNameOri)) {
            return null;
        }
        return "redirect:/czgl/list";
    }

    @DeleteMapping("/delete/{name}/{code}")
    public String delete(@PathVariable String name,@PathVariable String code) {
        //判断主键是否有一个为空
        if (StringUtils.isAnyBlank(name,code)) {
            //查询不到数据，直接返回
            return null;
        }
        //根据主键对删除数据
        if (!czylbService.removeByNameAndCode(name,code)) {
            return null;
        }
        return "redirect:/czgl/list";
    }


    @GetMapping("/searchWith")
    public String searchWith(CzSearchRequest czSearchRequest, Model model) {
        //判断用户是否有查询条件
        String staCode = czSearchRequest.getStaCode();
        String staName = czSearchRequest.getStaName();
        if (StringUtils.isAllBlank(staCode,staName)) {
            //查询为空，转发到列表接口
            return "forward:/czgl/list";
        }
        //根据条件查询数据
        List<Czylb> czylbs = czylbService.searchByCondition(czSearchRequest);
        model.addAttribute("czxx",czylbs);
        return "table/czxx_table";
    }
}
